#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h>

void* Consumer(void* args)
{
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    while(true)
    {
        //获取数据
        task_t t;
        bq->Pop(&t);
        //处理数据
        t();
    }
}

void* Productor(void* args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    while(true)
    {
        bq->Equeue(Task);
        std::cout << "Productor -> Task" << std::endl;
        sleep(1);
    }
}

int main()
{
    BlockQueue<task_t>* bq = new BlockQueue<task_t>();
    pthread_t c1,c2 ,p1,p2,p3;
    pthread_create(&c1, nullptr, Consumer, bq);
    pthread_create(&c2, nullptr, Consumer, bq);
    pthread_create(&p1, nullptr, Productor, bq);
    pthread_create(&p2, nullptr, Productor, bq);
    pthread_create(&p3, nullptr, Productor, bq);
    pthread_join(c1, nullptr);
    pthread_join(c2, nullptr);
    pthread_join(p1, nullptr);
    pthread_join(p2, nullptr);
    pthread_join(p3, nullptr);
    return 0;
}